// pages/question/comments.js
var app = getApp();
Page({
  data:{
      commentInput:'',
      answer:{},
      commonts:{},
       userInfo: {}
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            });
        });
    var data ={};
    data.Id = options.id;
    data.Qid = options.qid;
    data.Content = options.content;
        that.setData({
            answer: data
        });
    getComments(that,options.id);
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  comment:function(e){
      var that = this;
      var questionId=e.target.dataset.questionId;
      var answerId=e.target.dataset.answerId;
      if(!answerId){
          
      }
      console.log(this.data.commentInput)
      wx.request({
            url: 'https://api.cnblogs.com/api/questions/'+questionId+'/answers/'+answerId+'/comments', //仅为示例，并非真实的接口地址
            method: 'POST',
            data: {
                "Content": this.data.commentInput
            },
            header: {
                'Authorization': 'Bearer '+ app.accessToken
            },
            success: function (res) {
                getComments(that,answerId);
            },
            fail: function () {
                console.log('获取用户信息失败!');
            }
        });
  },
  delete:function(e){
      var that = this;
      var questionId=e.target.dataset.questionId;
      var answerId=e.target.dataset.answerId;
      var commentId=e.target.dataset.commentId;
      if(!answerId){
          
      }
      wx.request({
            url: 'https://api.cnblogs.com/api/questions/'+questionId+'/answers/'+answerId+'/comments/'+commentId, //仅为示例，并非真实的接口地址
            method: 'DELETE',
            data: {
                
            },
            header: {
                'Authorization': 'Bearer '+ app.accessToken
            },
            success: function (res) {
                getComments(that,answerId);
            },
            fail: function () {
                console.log('获取用户信息失败!');
            }
        });
  },
  listenCommentInput:function(event){
      this.data.commentInput = event.detail.value;
  }
});
//获取回答相关评论
function getComments(that, answer_id) {
    wx.request({
        url: 'https://api.cnblogs.com/api/questions/answers/' + answer_id + '/comments',
        method: 'GET',
        data: {
        },
        header: {
            'Authorization': 'Bearer ' + app.accessToken
        },
        success: function (res) {
            var data = {};
            data.status = true;
            data.data = res.data;
            if (res.data.length < 1) {
                data.status = false;
            }
            that.setData({
                commonts: data
            });
        },
        fail: function () {
            console.log('获取用户信息失败!');
        }
    });
}